import { createRouter, createWebHistory, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import MicroAppContainer from '../components/MicroAppContainer.vue'

// 定义路由配置
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: {
      title: '首页',
      keepAlive: true
    }
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue'),
    meta: {
      title: '关于我们'
    }
  },
  // 微应用路由配置
  {
    path: '/micro-auth/:path*',
    name: 'MicroAuth',
    component: MicroAppContainer,
    meta: {
      title: '认证中心',
      microApp: true
    }
  },
  {
    path: '/micro-dashboard/:path*',
    name: 'MicroDashboard',
    component: MicroAppContainer,
    meta: {
      title: '仪表盘',
      microApp: true
    }
  },
  {
    path: '/micro-user/:path*',
    name: 'MicroUser',
    component: MicroAppContainer,
    meta: {
      title: '用户管理',
      microApp: true
    }
  },
  {
    path: '/legacy/:path*',
    name: 'LegacyApp',
    component: MicroAppContainer,
    meta: {
      title: '传统应用',
      microApp: true
    }
  },
  // 404路由
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue'),
    meta: {
      title: '页面不存在'
    }
  }
]

// 创建路由器实例
const router = createRouter({
  history: createWebHistory(),
  // 开发环境使用哈希模式，生产环境使用history模式
  // history: process.env.NODE_ENV === 'development' ? createWebHashHistory() : createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 微前端平台`
  } else {
    document.title = '微前端平台'
  }

  // 这里可以添加权限验证逻辑
  next()
})

export default router